package com.ruoyi.web.controller.system;

import java.io.*;
import java.util.*;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.ruoyi.system.domain.Company;
import com.ruoyi.system.domain.TmcPay;
import com.ruoyi.system.domain.TmcUserHouse;
import com.ruoyi.system.service.ITmcHouseInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TmcPee;
import com.ruoyi.system.service.ITmcPeeService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 【費用管理】Controller
 *
 * @author ruoyi
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/system/pee")
public class TmcPeeController extends BaseController {
    @Autowired
    private ITmcPeeService tmcPeeService;
    @Autowired
    private ITmcHouseInfoService tmcHouseInfoService;

    /**
     * 查询【費用管理】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:pee:list')")
    @GetMapping("/list")
    public TableDataInfo list(TmcPee tmcPee) {
        startPage();
        List<TmcPee> list = tmcPeeService.selectTmcPeeList(tmcPee);
        return getDataTable(list);
    }

    /**
     * 查询小区管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:pee:list')")
    @GetMapping("/querycompanyList")
    public TableDataInfo querycompanyList() {
        startPage();
        List<Company> list = tmcPeeService.querycompanyList();
        return getDataTable(list);
    }

    //计算费用接口
    @GetMapping("/pay")
    public AjaxResult list(TmcPay tmcPay) {
        return toAjax(tmcPeeService.pay(tmcPay));
    }


    /**
     * 导出【費用管理】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:pee:export')")
    @Log(title = "【費用管理】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(Long companyId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        TmcUserHouse tmcUserHouse = new TmcUserHouse();
        tmcUserHouse.setCompanyId(companyId);
        List<TmcUserHouse> houses = tmcHouseInfoService.selectTmcHouseInfoList(tmcUserHouse);
        try {
            List<Object> list = new ArrayList<>();
            // 表一数据
            for (int i = 0; i < houses.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("ownername", houses.get(i).getOwnername());
                map.put("ownerphone", houses.get(i).getOwnerphone());
                map.put("ownerdaptname", houses.get(i).getOwnerdapt());
                map.put("ownerdapt", houses.get(i).getOwnerdapt());
                map.put("companyName", houses.get(i).getCompanyName());
                map.put("ownerdapt", houses.get(i).getOwnerdaptName());
                map.put("unitname", houses.get(i).getUnitname());
                map.put("housenum", houses.get(i).getHousenum());
                map.put("area", houses.get(i).getArea());
                map.put("num", tmcHouseInfoService.selectCarnum(houses.get(i).getOwnerphone(), houses.get(i).getCompanyId()));
                list.add(map);
            }
            // 表格使用的数据
            Map map = new HashMap();
            map.put("data", list);//放入表一数据
            // 获取模板文件
            //读取模板
            TemplateExportParams params = new TemplateExportParams("/home/ruoyi/uploadPath/excel/导出项目明细模板.xlsx");
            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "项目导入")
    @Log(title = "项目导入", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload")
    @CrossOrigin
    public AjaxResult Upload(MultipartFile file) {
        if (tmcPeeService.importExcel(file) > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("当月账单已上生成");
        }
    }

    /**
     * 获取【費用管理】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:pee:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tmcPeeService.selectTmcPeeById(id));
    }

    /**
     * 新增【費用管理】
     */
//    @PreAuthorize("@ss.hasPermi('system:pee:add')")
    @Log(title = "【費用管理】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmcPee tmcPee) {
        if (tmcPeeService.insertTmcPee(tmcPee) == 0) {
            return AjaxResult.error("该小区基数已存在");
        } else {
            return AjaxResult.success("新增成功");
        }
    }

    /**
     * 修改【費用管理】
     */
//    @PreAuthorize("@ss.hasPermi('system:pee:edit')")
    @Log(title = "【費用管理】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmcPee tmcPee) {
        if (tmcPeeService.updateTmcPee(tmcPee) == 0) {
            return AjaxResult.error("该小区基数已存在");
        } else {
            return AjaxResult.success("新增成功");
        }
    }

    @Log(title = "房屋产权信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tmcPeeService.deleteTmcPeeByIds(ids));
    }
}